package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.model.form.RoleForm;
import com.jinlia.show.modules.base.system.entity.SysRoleMenu;
import com.jinlia.show.modules.base.system.mapper.SysRoleMenuMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleMenuMapperService extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> {


    public List<SysRoleMenu> updateByRole(RoleForm roleForm) {
        if (roleForm.getId() != null) {
            this.lambdaUpdate()
                    .eq(SysRoleMenu::getRoleId, roleForm.getId())
                    .remove();
        }
        List<SysRoleMenu> collectSysRoleMenu = roleForm.getMenuIds().stream().map(menuId -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleForm.getId());
            sysRoleMenu.setMenuId(menuId);
            return sysRoleMenu;
        }).collect(Collectors.toList());
        this.saveBatch(collectSysRoleMenu);
        return collectSysRoleMenu;
    }
}
